package guitforl.builders

import scala.xml.NodeSeq
import javax.swing.JPanel
import guitforl.wrappers.{LayoutFactory, TabWrapper}

/**
 * Form = /tab+/
 * Tab = /grid/
 * Grid = /item+/
 * Item = /grid?/
 */
object TabBuilder {
  def buildTab(wrapTab: TabWrapper, node: NodeSeq) = {

    def buildGrid(parent: JPanel, node: NodeSeq): Unit = {
      val columns = node \ "col"
      val rows = node \ "row"
      if (!columns.isEmpty) {
        parent.setLayout(LayoutFactory.buildColLayout(columns))
        columns.foreach {
          column =>
            val nextPanel = new JPanel
            parent.add(nextPanel)
            buildGrid(nextPanel, column)
        }
      } else if (!rows.isEmpty) {
        parent.setLayout(LayoutFactory.buildRowLayout(rows))
        rows.foreach {
          column =>
            val nextPanel = new JPanel
            parent.add(nextPanel)
            buildGrid(nextPanel, column)
        }
      }
      else {
        parent.setLayout(LayoutFactory.createCellLayout)
        wrapTab.addComposite(((node \ "item") \ "@name").text, parent)
      }
    }

    buildGrid(wrapTab, node: NodeSeq)
  }


}